package com.agricultural.robot.observer;

import com.agricultural.robot.model.EnvironmentData;

import java.util.ArrayList;
import java.util.List;

/**
 * 农田监测器 - 观察者模式
 */
public class FarmlandMonitor implements Subject {
    private List<Observer> observers;
    private EnvironmentData environmentData;

    public FarmlandMonitor() {
        observers = new ArrayList<>();
        environmentData = new EnvironmentData();
    }

    @Override
    public void registerObserver(Observer o) {
        observers.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        observers.remove(o);
    }

    @Override
    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update(environmentData);
        }
    }

    public void setSoilMoisture(int moisture) {
        environmentData.setSoilMoisture(moisture);
        System.out.println("土壤湿度更新为: " + moisture + "%");
        notifyObservers();
    }

    public void setTemperature(double temperature) {
        environmentData.setTemperature(temperature);
        System.out.println("温度更新为: " + temperature + "°C");
        notifyObservers();
    }

    public void setCropGrowthStatus(String status) {
        environmentData.setCropGrowthStatus(status);
        System.out.println("作物生长状态更新为: " + status);
        notifyObservers();
    }

    public void setWindSpeed(double windSpeed) {
        environmentData.setWindSpeed(windSpeed);
        System.out.println("风速更新为: " + windSpeed + "m/s");
        notifyObservers();
    }
}
